import { chunkArray, randomUid, formatDate } from '../utils';
import { getRandomForArr, now, randomInt, names } from './util';

//  ---------------- user -------------

const roles = [
  {
    id: '1',
    name: '普通用户',
    variable: 'USER',
    createTime: now,
    updateTime: now,
    isDefault: true
  },
  {
    id: '2',
    name: '管理员',
    variable: 'ADMIN',
    createTime: now,
    updateTime: now,
    isDefault: false
  },
  {
    id: '3',
    name: '运营',
    variable: 'OP',
    createTime: now,
    updateTime: now,
    isDefault: false
  },
  {
    id: '4',
    name: '商家',
    variable: 'STORE',
    createTime: now,
    updateTime: now,
    isDefault: false
  }
];

const users = names.map((name) => {
  return {
    id: randomUid(),
    name,
    avatar: `https://picsum.photos/id/${randomInt()}/80/80`,
    account: 'admin1564',
    roles: roles[0].id,
    phone: '138****8888',
    online: getRandomForArr(['0', '1']),
    createTime: now
  };
});

var result;

const filterUsers = (params): any[] => {
  let filteredUsers = [...users];
  if (params.userName) {
    filteredUsers = filteredUsers.filter((user) => user.name.indexOf(params.userName) >= 0);
  }
  if (params.phone) {
    filteredUsers = filteredUsers.filter((user) => user.phone.indexOf(params.phone) >= 0);
  }

  return chunkArray(filteredUsers, params.pageSize);
};

export const getUserList = (params): Promise<any> => {
  return new Promise<any>((resolve) => {
    setTimeout(() => {
      result = filterUsers(params);
      resolve({
        data: result[params.pages - 1],
        total: result.reduce((v, o) => (v += o.length), 0)
      });
    }, 500);
  });
};

export const addUser = (params): Promise<any> => {
  return new Promise<void>((resolve) => {
    setTimeout(() => {
      users.push({
        ...params,
        avatar: `https://picsum.photos/id/${randomInt()}/80/80`,
        status: getRandomForArr(['1', '2']),
        createTime: now,
        id: randomUid()
      });
      resolve();
    }, 300);
  });
};

export const deleteUser = (id): Promise<any> => {
  return new Promise<void>((resolve) => {
    setTimeout(() => {
      let idx = users.findIndex((v) => v.id === id);
      if (idx >= 0) {
        users.splice(idx, 1);
      }
      resolve();
    }, 300);
  });
};

export const updateUser = (params): Promise<any> => {
  return new Promise<void>((resolve) => {
    setTimeout(() => {
      let idx = users.findIndex((v) => v.id === params.id);
      Object.keys(params).forEach((key) => {
        users[idx][key] = params[key];
      });
      resolve();
    }, 300);
  });
};

//  ---------------- role -------------

export const getRoles = (params): Promise<any> => {
  return new Promise<any>((resolve) => {
    setTimeout(() => {
      let result = chunkArray(roles, params.pageSize);
      resolve({
        data: result[params.pages - 1],
        total: roles.length
      });
    }, 300);
  });
};

export const changeDefaultRole = (params): Promise<any> => {
  return new Promise<void>((resolve, reject) => {
    setTimeout(() => {
      let idx = roles.findIndex((v) => v.id === params.id);
      if (params.isDefault) {
        for (const key in roles) {
          roles[key].isDefault = false;
        }
        roles[idx].isDefault = true;
      } else {
        if (roles[idx].isDefault) {
          return reject({
            msg: '至少需要设置一个默认角色'
          });
        }
      }
      resolve();
    }, 300);
  });
};

export const addRole = (params): Promise<any> => {
  return new Promise<void>((resolve) => {
    setTimeout(() => {
      if (params.isDefault) {
        for (const key in roles) {
          roles[key].isDefault = false;
        }
      }
      let role = {
        id: randomUid(),
        createTime: now,
        updateTime: now,
        ...params
      };
      roles.push(role);
      resolve();
    }, 300);
  });
};

export const deleteRole = (id): Promise<any> => {
  return new Promise<void>((resolve, reject) => {
    setTimeout(() => {
      let idx = roles.findIndex((v) => v.id === id);
      if (idx >= 0) {
        if (roles[idx].isDefault) {
          return reject({
            msg: '该角色是默认角色，请先修改默认角色'
          });
        }
        roles.splice(idx, 1);
      }
      resolve();
    }, 300);
  });
};

export const updateRole = (params): Promise<any> => {
  return new Promise<void>((resolve, reject) => {
    setTimeout(() => {
      let idx = roles.findIndex((v) => v.id === params.id);
      if (params.isDefault) {
        for (const key in roles) {
          roles[key].isDefault = false;
        }
      } else {
        if (roles[idx].isDefault) {
          return reject({
            msg: '至少需要设置一个默认角色'
          });
        }
      }
      Object.keys(params).forEach((key) => {
        roles[idx][key] = params[key];
      });
      roles[idx].updateTime = formatDate(new Date());
      resolve();
    }, 300);
  });
};
